import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import axios from 'axios'
//利用axios的拦截器,每个请求加上请求头
axios.interceptors.request.use(config => {
  //把jwt放到请求头中 
  let jwt = localStorage.getItem('authorization');
  if (jwt) {
    config.headers.authorization = jwt
  }
  return config;
})
Vue.prototype.$axios = axios


axios.interceptors.response.use(async (response) => {
  if (response.data.code == 2000) {
    await router.push('/login');
  } else if (response.data.code == 500) {
    await router.push('/error');
  } 
  return response;
});

Vue.use(ElementUI)
Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
